// grunt入口文件

// 实现这个项目的构建任务

const sass = require('sass')
const loadGruntTasks = require('load-grunt-tasks')

module.exports = grunt => {
  
    const project = {

        dirs: {
            input     : 'test/input',
            output    : 'dist',
            temp      : 'temp'
        },

        files: {
            scripts     : '/scripts',
            vendor      : '/scripts/vendor',
            any         : '/**/*',
            thisDir     : '/*',
            dot         : {
                javascript  : '.js',
                html        : '.html'
            }
        }
    }

    //配置
    grunt.initConfig({
        project: project,
        clean:{
            dist:'dist'
        },
        //sass 编译
        sass: {
            options:{
                implementation:sass
            },
            main: {
                files:  {'dist/assets/styles/main.css':'src/assets/styles/main.scss'}
            }
        },
        // babel
        babel:{
            main: {
                options: {
                    presets: ['@babel/preset-env']
                },
                files: {
                    'dist/assets/scripts/main.js':'src/assets/scripts/main.js'
                }
            }
        },
        // 热更新
        watch: {
            js: {
                files:['src/assets/scripts/*.js'],
                tasks:['babel']
            },
            css: {
                files:['src/assets/styles/*.scss'],
                tasks:['sass']
            }
        },
        // 模板编译
        swigtemplates: {
            options:{
                defaultContext: {
                    test:'赛车',
                    menus:[
                        {
                            name: 'Home',
                            icon: 'aperture',
                            link:'index.html'
                        },
                        {
                            name:'About',
                            link: 'about.html'
                        },
                        {
                            name: 'Contact',
                            link:'#',
                            children: [
                                {
                                    name: 'Twitter',
                                    link:'https://twitter.com/w_zce'
                                } 
                            ]
                        }
                    ],
                    pkg:require('./package.json'),
                    date: new Date()
                  },
                  templatesDir :'src' 
            },           
            mySite: {
              dest: 'dist/',
              src: ['src/*.html']
            }
          },
         //css 压缩
         cssmin: {
            target: {
              files: [{
                expand: true,
                cwd: 'dist/assets/styles',
                src: ['*.css', '!*.min.css'],
                dest: 'dist/assets/styles',
                ext: '.css'
              }]
            }
          },
        //   js 压缩
        uglify: {
            my_target: {
              files: {
                'dist/assets/scripts/main.js': 'dist/assets/scripts/main.js'
              }
            }
          },
        //html 压缩
        htmlmin: {                                     // Task
            dist: {                                      // Target
              options: {                                 // Target options
                removeComments: true,
                collapseWhitespace: true
              },
              files: {                                   // Dictionary of files
                'dist/index.html': 'dist/index.html',     // 'destination': 'source'
                'dist/about.html': 'dist/about.html'
              }
            }
          },
      
        // useref
        useref: {
            html: project.dirs.output + project.files.any + project.files.dot.html,

            temp: project.dirs.output
        },
          //concat
          concat: {
            basic_and_extras: {
              files: {
                'dist/assets/scripts/vendor.js': ['node_modules/jquery/dist/jquery.js', 'node_modules/popper.js/dist/umd/popper.js', 'node_modules/bootstrap/dist/js/bootstrap.js'],
                'dist/assets/styles/vendor.css': ["node_modules/bootstrap/dist/css/bootstrap.css"],
              },
            },
          },
          //copy
          copy: {
            main: {
              files: [
                // includes files within path
                {expand: true, cwd: 'public/',  src: '*', dest: 'dist/', filter: 'isFile'},
                {
                    expand: true,
                    cwd: 'src/assets/',
                    src: 'fonts/*',
                    dest: 'dist/assets/',
                  },
                  {
                    expand: true,
                    cwd: 'src/assets/',
                    src: 'images/*',
                    dest: 'dist/assets/',
                  },
              ],
            },
          },
        //   测试服务器
          browserSync: {
            bsFiles: {
                src : '**/*'
            },
            options: {
                server: {
                    baseDir: ['dist','src','public']
                }
            }
        }
    })
    

    // 载入插件
    // grunt.loadNpmTasks('grunt-sass')
    // grunt.loadNpmTasks('grunt-contrib-clean')
    loadGruntTasks(grunt) //自动加载所有 grunt 插件




    //默认任务

    grunt.registerTask('build',['clean','sass','babel','swigtemplates','concat','useref','copy','uglify','cssmin','htmlmin'])

    grunt.registerTask('dev',['clean','sass','babel','swigtemplates','concat','useref','browserSync'])

}